package homework.view;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import homework.dao.Addimformation;
import homework.entity.User;
import homework.service.Usermanage;

public class Login extends JFrame{
	private JLabel l_name,l_password,l_type;
	private JButton b_login,b_clear,b_register,b_import;
	private JTextField t_nameField;
	private JPasswordField p_password;
	private JComboBox<String> c_typeBox = new JComboBox<>();;
	
	
	public Login() {
		this.setSize(400,300);
        this.setTitle("快递物流平台-登录");
        this.setLocation(300,200);
        init();
        this.setVisible(true);
	}
	public void init() {
		this.setLayout(null);
		l_name = new JLabel("用户名:",(int) CENTER_ALIGNMENT);
		l_password = new JLabel("密码:",(int)CENTER_ALIGNMENT);
		l_type = new JLabel("身份",(int)CENTER_ALIGNMENT);
		t_nameField = new JTextField();
		p_password = new JPasswordField();
		c_typeBox.addItem("用户");
		c_typeBox.addItem("快递员");
		b_clear = new JButton("清空");
		b_import = new JButton("直接导入");
		b_import.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				Addimformation am=new Addimformation();
				am.add();
			}
		});
		b_clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				t_nameField.setText("");
				p_password.setText("");
				
			}
		});
		b_login = new JButton("登录");
		b_login.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String s2;
				Usermanage um = new Usermanage();
				User user = um.login(t_nameField.getText(),p_password.getPassword(),(String)c_typeBox.getSelectedItem());
				
				String s=(String)c_typeBox.getSelectedItem();
				if(user!=null && (s.equals("用户"))) {
					s2="登录成功";
					new QueryFrame(user);
					
				}else if(user!=null&&(s.equals("快递员"))) {
					s2="登录成功";
					new CreateFrame();
					
				}else {
					
					s2="登录失败，用户名或密码错误";
					
				}
				JOptionPane.showMessageDialog(null, s2);
			}
		});
		b_register = new JButton("注册");
		b_register.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				new RegisterFrame();
			}
		});
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3,2,5,5));
		p.add(l_name);
		p.add(t_nameField);
		p.add(l_password);
		p.add(p_password);
		p.add(l_type);
		p.add(c_typeBox);
		
		p.setBounds(5,5,375,120);
		this.add(p);
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(1,4,5,5));
		p2.add(b_clear);
		p2.add(b_import);
		p2.add(b_login);
		p2.add(b_register);
		p2.setBounds(5,190,375,60);
		this.add(p2);
		
		
	}
	

}
